package com.loginconsumer.controller;

import com.loginconsumer.service.Loginconsumerservice;
import dto.Dto;
import dto.GetAdmin;
import dto.RedisValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import pojo.Admin;
import pojo.User;

import javax.annotation.Resource;

import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
public class LoginconsumerController {

    @Autowired
    RedisTemplate<Object,Object> redisTemplate;

    @Resource
    private Loginconsumerservice loginconsumerservice;

    /**根据用户账号查询用户信息*/
    @RequestMapping("/selectuser")
    public Dto<RedisValue> selectuser(@RequestBody Map<String,Object> map){
        Dto dto=new Dto<>();

        System.out.println("username="+map.get("name"));
        String name= (String) map.get("name");
        RedisValue redisValue=new RedisValue();
        if(loginconsumerservice.selectadmin(name).getObj()!=null){
            //用户存在
            dto= loginconsumerservice.selectadmin(name);
            Admin admin= (Admin) dto.getObj();
            String passWord=admin.getPassword();
            if(passWord.equals(map.get("password"))){
                //通过
                dto.setSuccess(true);
                dto.setCode(0);

                String redisKey= UUID.randomUUID().toString();
                redisValue.setKey(redisKey);
                redisValue.setUsername(admin.getUsername());

                StringBuffer stringBuffer=new StringBuffer(admin.getId().toString());

                System.out.println(stringBuffer);

                //根据管理员id获取权限
                List list=loginconsumerservice.selectAdminPer(admin.getId().toString());
                stringBuffer.append("|"+admin.getUsername());
                //将list集合遍历加到数据上
                for(int i=0;i<list.size();i++){
                    stringBuffer.append("|"+ list.get(i));
                }
                System.out.println(stringBuffer);
                redisTemplate.opsForValue().set(redisKey,stringBuffer,300, TimeUnit.MINUTES);
                //页面传的值
                redisValue.setUsername(stringBuffer.toString());
                dto.setObj(redisValue);
            }else{
                //密码错误
                dto.setCode(2);
                dto.setSuccess(false);
            }
        }else{
            //用户不存在
              dto.setMsg("账户不存在");
              dto.setSuccess(false);
              dto.setCode(1);
        }
        return dto;
    }

    @RequestMapping("/del")
    public Boolean del(){
        //前台获取key 以及value
        //获取zuul提供的请求上下文对象
//        RequestContext requestContext = RequestContext.getCurrentContext() ;
//        //从上下文中获取request对象
//        HttpServletResponse response = requestContext.getResponse() ;
//        HttpServletRequest request = requestContext.getRequest() ;
//        if( null!= CookieUtils.getCookieByName(request, "token")){
//            Cookie cookie = CookieUtils.getCookieByName(request, "token");
//            String key=cookie.getValue().substring(0,cookie.getValue().indexOf("|"));
//            redisTemplate.delete(key);
//        }

        return true;
    }

    @RequestMapping("/getAdmin1")
    public List getAdmin1(@RequestBody  Map <String,Object> map){
        List list=loginconsumerservice.getAdmin1(map);
        return list;
    }

    @RequestMapping("/updateAdmin")
     public int updateAdmin(@RequestBody  List<GetAdmin> list){
        return loginconsumerservice.updateAdmin(list);
    }


}
